//
//  sendMessageViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/20.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "sendMessageViewController.h"
#import "XCCutAvatarsViewController.h"
#import "SKPhotoActionSheet.h"
#import "sendMessageMaskView.h"
#import "sendTopViewController.h"
#import "MBProgressHUD+MJ.h"
@interface sendMessageViewController () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imgCategory;
@property (weak, nonatomic) IBOutlet UILabel *lblPlacehoulder;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *btnImg1;
@property (weak, nonatomic) IBOutlet UIButton *btnImg2;
@property (weak, nonatomic) IBOutlet UIButton *btnImg3;

@property (nonatomic, strong) sendMessageMaskView *maskView;
@property (nonatomic,strong)SKPhotoActionSheet*photoActionSheet;
@property (nonatomic,strong)XCCutAvatarsViewController *cutVC;
@end

@implementation sendMessageViewController
#pragma mark - viewLife
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIImage *image = [UIImage imageNamed:@"transparent-1"];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];
    
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
- (IBAction)btnResetClick:(id)sender {
    self.textView.text = @"";
    [MBProgressHUD showSuccess:@"重置成功"];
}
- (IBAction)btnPublishClick:(id)sender {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.y = 0;
    } completion:^(BOOL finished) {
//        if (finished) {
//            [self.maskView removeFromSuperview];
//        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view bringSubviewToFront:self.lblPlacehoulder];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"arrow_back" highImage:@"arrow_back" target:self action:@selector(leftBtnClick)];
    self.title = @"发布";
    self.textView.delegate = self;
    
    sendMessageMaskView *maskView = [[[NSBundle mainBundle]loadNibNamed:@"sendMessageMaskView" owner:nil options:nil]lastObject];
    maskView.frame = CGRectMake(0, GYMScreenH, self.view.width, self.view.height);
    [maskView.btnTop addTarget:self action:@selector(maskBtnTopClick) forControlEvents:UIControlEventTouchUpInside];
    [maskView.btnCancel addTarget:self action:@selector(maskBtnCancelClick:) forControlEvents:UIControlEventTouchUpInside];
    self.maskView = maskView;
    
    [self.view addSubview:maskView];
}


#pragma mark - action
- (void)maskBtnTopClick{
    sendTopViewController *vc = [[sendTopViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)maskBtnCancelClick:(UIButton *)button{
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.y = GYMScreenH;
    } completion:^(BOOL finished) {
        if (finished) {
//            [self.maskView removeFromSuperview];
        }
    }];
}
- (void)leftBtnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length > 0) {
        self.lblPlacehoulder.hidden = YES;
    }else{
        self.lblPlacehoulder.hidden = NO;
    }
}
- (IBAction)btnCancelClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)btnAddPicClick:(UIButton *)sender {
    __weak typeof(self) wself = self;
    
    [wself.photoActionSheet showWithSender:wself animate:YES completion:^(NSArray<UIImage *> * _Nonnull selectPhotos) {
        if(self.btnImg2.currentBackgroundImage == nil){// 一个都没选
            
            if (selectPhotos.count == 1) {
                [self.btnImg1 setBackgroundImage:selectPhotos[0] forState:UIControlStateNormal];
                [self.btnImg2 setBackgroundImage:[UIImage imageNamed:@"publish_sendActive_add"] forState:UIControlStateNormal];
    //            self.cutVC = [[XCCutAvatarsViewController alloc]initWithImage:selectPhotos[0]];
                
    //            [self.navigationController showViewController:self.cutVC sender:self];
                
            }else if (selectPhotos.count == 2){
                
                [self.btnImg1 setBackgroundImage:selectPhotos[0] forState:UIControlStateNormal];
                [self.btnImg2 setBackgroundImage:selectPhotos[1] forState:UIControlStateNormal];
                [self.btnImg3 setBackgroundImage:[UIImage imageNamed:@"publish_sendActive_add"] forState:UIControlStateNormal];
            }else if (selectPhotos.count == 3){
                [self.btnImg1 setBackgroundImage:selectPhotos[0] forState:UIControlStateNormal];
                [self.btnImg2 setBackgroundImage:selectPhotos[1] forState:UIControlStateNormal];
                [self.btnImg3 setBackgroundImage:selectPhotos[2] forState:UIControlStateNormal];
            }
        }
        else if (self.btnImg2.currentBackgroundImage == [UIImage imageNamed:@"publish_sendActive_add"]){
            
        }
        
    }];
}
- (IBAction)btnSendClick:(id)sender {
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (SKPhotoActionSheet *)photoActionSheet{
    if (_photoActionSheet == nil) {
        _photoActionSheet = [[SKPhotoActionSheet alloc] init];
        _photoActionSheet.maxSelectCount = 3;
        _photoActionSheet.maxPreviewCount = 5;
    }
    return _photoActionSheet;
}
@end
